import process from 'node:process'
import { createLogger as createWinstonLogger, format, transports } from 'winston'

export function createLogger(serviceName: string) {
  const logger = createWinstonLogger({
    level: 'info',
    format: format.combine(
      format.timestamp({
        format: 'YYYY-MM-DD HH:mm:ss',
      }),
      format.errors({ stack: true }),
      format.splat(),
      format.json(),
    ),
    defaultMeta: { service: serviceName },
    transports: [
      //
      // - Write to all logs with level `info` and below to `quick-start-combined.log`.
      // - Write all logs error (and below) to `quick-start-error.log`.
      //
      new transports.File({ filename: `${serviceName}-error.log`, level: 'error' }),
      new transports.File({ filename: `${serviceName}-combined.log` }),
    ],
  })

  //
  // If we're not in production then **ALSO** log to the `console`
  // with the colorized simple format.
  //
  if (process.env.NODE_ENV !== 'production') {
    logger.add(new transports.Console({
      format: format.combine(
        format.colorize(),
        format.simple(),
      ),
    }))
  }

  return logger
}
